package clh;

import static org.junit.Assert.assertTrue;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IRobotSnapshot;
//import robocode.control.snapshot.RobotState;
import robocode.control.testing.RobotTestBed;

/**
 * Tests that Chuan should not turn more than 5 degrees each turn since Chuan circles around
 * SittingDuck every turn the heading should only change by a little.
 * 
 * @author Chuan Lun Hung
 * 
 */
public class TestChuanHeading extends RobotTestBed {

  /** initialize correctBearing to true, change it to false whenever error is found. */
  boolean correctBearing = true;
  /** to save Chuan's previous heading for comparison. */
  double previousHeading = 0;

  /**
   * Specifies that SittingDuck and Chuan are to be matched up in this test case.
   * 
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.SittingDuck,clh.Chuan";
  }

  /**
   * This test runs for 10 round.
   * 
   * @return The number of rounds.
   */
  @Override
  public int getNumRounds() {
    return 10;
  }

  /**
   * Set initial positions of two robots.
   * 
   * @return The initial positions of two robots in String
   */
  @Override
  public String getInitialPositions() {
    return "0,0,0,0,100,90";
  }

  /**
   * Chuan should not turn more than 5 degree in each turn.
   * 
   * @param event Info about the current state of the battle.
   */
  @Override
  public void onTurnEnded(TurnEndedEvent event) {

    IRobotSnapshot robot = event.getTurnSnapshot().getRobots()[1];
    double heading = robot.getBodyHeading();
    double difference = heading - previousHeading;

    if (difference < -5 || difference > 5) {
      correctBearing = false;
    }
    // update previousHeading
    previousHeading = robot.getBodyHeading();
  }

  /**
   * After the battle, check to see that we have no errors.
   * 
   * @param event Details about the completed battle.
   */
  @Override
  public void onBattleCompleted(BattleCompletedEvent event) {
    assertTrue(correctBearing);
  }
}
